from rest_framework import serializers
from .models import Book, Publish, Author, AuthorDetail


class PublishSerializer(serializers.ModelSerializer):
    '''出版社表 ModelSerializer类'''

    class Meta:
        model = Publish
        fields = ['name', 'addr', 'email']
        extra_kwargs = {
            'name': {'max_length': 8, 'min_length': 3},
            'addr': {'max_length': 128, 'min_length': 5},
        }


class AuthorSerializer(serializers.ModelSerializer):
    '''作者表 ModelSerializer类'''

    class Meta:
        model = Author
        fields = ['name', 'age', 'sex', 'phone', 'addr']
        extra_kwargs = {
            'sex': {"default": 'male'},
        }

    phone = serializers.CharField(source='detail.phone', max_length=11)
    addr = serializers.CharField(source='detail.addr', max_length=128)

    def create(self, validated_data):
        # print(validated_data)
        detail_data = validated_data.pop('detail')
        detail = AuthorDetail.objects.create(**detail_data)
        author = Author.objects.create(**validated_data, detail=detail)
        return author

    def update(self, instance, validated_data):
        print(validated_data)
        phone = validated_data.pop('phone')
        addr = validated_data.pop('addr')
        for k, v in validated_data.items():
            setattr(instance, k, v)
        instance.save()
        detail = AuthorDetail.objects.filter(author=instance)
        detail.update(phone=phone, addr=addr)
        detail.save()
        return instance


class BookSerializer(serializers.ModelSerializer):
    '''书籍表 ModelSerializer类'''

    class Meta:
        model = Book
        fields = ['id', 'title', 'price', 'publish', 'authors', 'publish_detail', 'authors_list']
        extra_kwargs = {
            'id': {'required': False},
            'title': {'max_length': 8, 'min_length': 3},
            'price': {'min_value': 10, 'max_value': 999},
            'publish': {'write_only': True},
            'authors': {'write_only': True}
        }

    def create(self, validated_data):
        publish = validated_data.pop('publish')
        authors = validated_data.pop('authors')
        book = Book.objects.create(**validated_data, publish=publish)
        book.authors.add(*authors)
        return book

    def update(self, instance, validated_data):
        # print(validated_data)
        publish = validated_data.pop('publish')
        authors = validated_data.pop('authors')
        for k, v in validated_data.items():
            setattr(instance, k, v)
        instance.publish = publish
        instance.authors.set(authors)
        instance.save()
        return instance

    publish_detail = PublishSerializer(read_only=True, source='publish')
    authors_list = AuthorSerializer(read_only=True, source='authors', many=True)


class UserLogSerializer(serializers.Serializer):
    ip = serializers.CharField()
    visit_time = serializers.DateTimeField()
    method = serializers.CharField()
    path = serializers.CharField()
    user_agent = serializers.CharField()
    browser = serializers.CharField()
    version = serializers.CharField()
